package com.angel.controller;

import com.angel.bean.Webresource;
import com.angel.exception.StatusException;
import com.angel.service.OtherlogService;
import com.angel.service.WebresourceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/webResourceController")
public class WebResourceController {

    @Resource
    private WebresourceService webresourceService;
    @Autowired
    private OtherlogService otherlogService;


    /**
     * 删除
     *
     * @param webresource
     * @return
     */
    @RequestMapping("/delWebresource")
    @ResponseBody
    public Object delWebresource(Webresource webresource) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        int result = webresourceService.delete(webresource);
        if (result != 0) {
            map.put("result", "success");
        }

        return map;
    }

    /**
     * 添加
     *
     * @param webresource
     * @return
     */
    @RequestMapping("/addWebresource")
    @ResponseBody
    public Object addWebresource(Webresource webresource) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        webresource.setUploaddate(new Timestamp(System.currentTimeMillis()));
        webresource.setState(1);
        int result = webresourceService.insert(webresource);
        if (result != 0) {
            map.put("result", "success");
        }

        return map;
    }

    /**
     * 更新
     *
     * @param webresource
     * @return
     */
    @RequestMapping("/updateWebresource")
    @ResponseBody
    public Object updateWebresource(Webresource webresource) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        int result = webresourceService.update(webresource);
        if (result != 0) {
            map.put("result", "success");
        }

        return map;
    }

    /**
     * 查询单个
     *
     * @param webresource
     * @return
     */
    @RequestMapping("/findOneWebresource")
    @ResponseBody
    public Object findOneWebresource(Webresource webresource) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        webresource.setState(1);
        Webresource webres = webresourceService.findOne(webresource);
        if (webres != null) {
            map.put("webres", webres);
        }

        return map;
    }

    /**
     * 查询所有资源
     *
     * @param model
     * @return  list
     */
    @RequestMapping("/findWebresource")
    @ExceptionHandler(value = StatusException.class)
    public String findWebresource(@RequestParam(value = "pn", defaultValue = "1") String pn, Model model) {

        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        PageHelper.startPage(pageNo, 5);
        Webresource webresource = new Webresource();
        webresource.setState(1);

        List<Webresource> webresourceList = webresourceService.findWebresource(webresource);

        PageInfo pageInfo = new PageInfo(webresourceList);
        if (pageInfo != null) {
            model.addAttribute("pageInfo", pageInfo);
        }
        return "webResourceEntry";
    }

    /**
     * 模糊查询
     *
     * @param pn
     * @param model
     * @return
     */
    @RequestMapping("/searchWebresource")
    @ExceptionHandler(value = StatusException.class)
    public String searchWebresource(@RequestParam(value = "pn", defaultValue = "1") String pn,
                                 @RequestParam(value = "type") String type,
                                 @RequestParam(value = "content") String content,
                                 Model model) {
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        content = content.trim().replaceAll(" ", "");
        PageHelper.startPage(pageNo, 5);
        Webresource webresource;
        List<Webresource> webresourceList = null;
        switch (type) {
            case "rname":
                // 名称
                webresource = new Webresource();
                webresource.setRname(content);
                webresourceList = webresourceService.searchWebresource(webresource);
                break;
            case "reinstruct":
                // 说明
                webresource = new Webresource();
                webresource.setReinstruct(content);
                webresourceList = webresourceService.searchWebresource(webresource);
                break;
            default:
                break;
        }
        PageInfo pageInfo = new PageInfo(webresourceList);

        model.addAttribute("type", type);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("content", content);
        return "webResourceEntry2";
    }
}
